<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 按钮 -->
      <el-card shadow="never">

        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          id="Turnofpage"
          v-show="$route.query.id"
        >
          <!-- <el-breadcrumb-item :to="{ path: '/subjects/directorys' }">学科科目</el-breadcrumb-item> -->
          <el-breadcrumb-item>学科科目</el-breadcrumb-item>
          <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="btn_wrapper">
          <el-form
            label-width="80px"
            size="small"
          >
            <el-row>
              <el-form :inline="true">

                <!-- 搜索框 -->
                <el-col :span="7">
                  <el-form-item label="标签名称">
                    <el-input
                      placeholder="根据标签搜索"
                      v-model="tagName"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <!-- 下拉选项 -->
                <el-col :span="11">
                  <el-form-item label="状态">
                    <el-select
                      placeholder="请选择"
                      size="small"
                      v-model="state"
                    >
                      <el-option
                        label="启用"
                        value="1"
                      ></el-option>
                      <el-option
                        label="禁用"
                        value="0"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                  <el-button
                    size="small"
                    @click="shanchusousuo"
                  >删除</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="serch"
                  >搜索</el-button>
                </el-col>
                <el-col
                  :span="3"
                  v-show="$route.query.id"
                >
                  <div id="search">
                    <i class="el-icon-back"></i>
                    <el-link
                      :underline="false"
                      type="primary"
                      @click="returndiscipline"
                    >返回学科</el-link>
                  </div>
                </el-col>

                <!-- 新增按钮 -->
                <el-col :span="1">
                  <el-button
                    icon="el-icon-edit"
                    type="success"
                    size="small"
                    @click="addEmployeeFn"
                  >新增标签</el-button>
                </el-col>
              </el-form>
            </el-row>

            <!--  -->
            <!-- :title="alertText" -->
            <el-alert
              :title="alertText"
              type="info"
              class="alert"
              show-icon
              :closable="false"
            >
            </el-alert>
            <!-- 获取内容表单 -->

            <el-table
              :data="list"
              v-loading="eloading"
            >
              <el-table-column
                label="序号"
                type="index"
              />
              <el-table-column
                label="学科名称"
                prop="subjectName"
              />
              <el-table-column
                label="标签名称"
                prop="tagName"
              />
              <el-table-column
                label="创建者"
                prop="username"
              />
              <el-table-column
                label="创建日期"
                prop="addDate"
                :formatter="formatterFn"
              />
              <!-- 状态 -->

              <el-table-column
                label="状态"
                prop="state"
                :formatter="formatData"
              />

              <el-table-column
                label="操作"
                fixed="right"
                width="280"
              >
                <template #default="{ row }">
                  <el-button
                    type="text"
                    size="edium"
                    @click="buttonopen(row)"
                  >{{row.state? "禁用" :"启用"}}</el-button>

                  <el-button
                    title="修改"
                    type="text"
                    size="edium"
                    :disabled="disabledata(row)"
                    @click="xiugai(row)"
                  >修改</el-button>
                  <el-button
                    type="text"
                    size="edium"
                    :disabled="disabledata(row)"
                    @click="delEmployee(row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div style="height: 60px; margin-top: 10px;margin-left: 1200px">

              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="page"
                :page-sizes="[5,20,30,40]"
                :page-size="pagesize"
                layout="total, prev, pager, next, jumper,sizes"
                :total="counts"
              >
              </el-pagination>

            </div>
            <!-- 新增弹框 -->
            <tags-add
              :show-dialog.sync="showDialog"
              :listform="listform"
            />

          </el-form>
        </div>
      </el-card>
      <!-- 搜索 -->

    </div>
  </div>
</template>
<script>
import { list, remove, changeState, detail } from '@/api/hmmm/tags.js'

import * as dayjs from 'dayjs'
import TagsAdd from '../components/tags-add.vue'
import { status } from '@/api/hmmm/constants.js/'
export default {
  name: 'tags',
  data () {
    return {
      alertText: '',
      total: 0,
      status: status,
      listform: {},
      list: [],
      dataList: [],
      page: 1, // 当前页
      pages: 2, // 当前页
      pagesize: 10, // 每页条数
      counts: 0, // 总条数
      value: '',
      eloading: false,
      showDialog: false,
      kan: '',

      form: {
        subjectID: '', // 学科id
        tagName: '' // 标签名称
      },
      tagName: '',
      state: ''
    }
  },

  components: {
    TagsAdd
  },
  created () {
    // 获取角色列表
    if (this.$route.query.id) {
      this.getRoleList(null, null, this.$route.query.id)
    } else {
      this.getRoleList()
    }
  },
  methods: {
    disabledata (row) {
      if (row.state === 1) {
        return true
      } else {
        return false
      }
    },
    // 按钮禁用
    async buttonopen (row) {
      if (row.state === 0) {
        this.$message.success('启用成功')
        row.state = 1
      } else {
        row.state = 0
        this.$message.success('禁用成功')
      }
      await changeState({ id: row.id, state: row.state })
    },
    // 搜索
    async serch () {
      await this.getRoleList(this.tagName, this.state)
    },
    async shanchusousuo () {
      await this.getRoleList()
      this.tagName = ''
      this.state = ''
    },
    //
    async xiugai (row) {
      this.showDialog = true
      const { data } = await detail(row)
      this.listform = data
      this.getRoleList()
    },
    formatData (row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      if (cellValue === 1) {
        return '启用'
      }
      if (cellValue === 0) {
        return '禁用'
      }
    },
    addEmployeeFn () {
      this.showDialog = true
    },
    formatterFn (row, column, cellValue, index) {
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getRoleList()
    },
    delEmployee (row) {
      this.$confirm('确定要删除该员工?', '温馨提示')
        .then(async () => {
          await remove(row)
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.$message.success('删除成功')
          this.getRoleList()
        })
        .catch(() => {
          console.log('取消')
        })
    },
    async getRoleList (tagName, state, subjectID) {
      this.eloading = true
      const {
        data: { counts, items }
      } = await list({
        page: this.page,
        pagesize: this.pagesize,
        state,
        tagName: tagName,
        subjectID
      })
      this.eloading = false
      this.list = items
      this.counts = counts
      this.total = counts
      this.alertText = `共 ${this.total} 条记录`
    },
    returndiscipline () {
      this.$router.push('/subjects/list')
    }
  }
}
</script>
<style lang="scss" scoped>
.btn_wrapper {
  background-color: #fff;
}
//最好不要直接使用这个.el-button,因为一设置其它页面有影印
// .el-button {
//   margin-top: 15px;
// }
.el-col {
  margin-top: 14px;
}

.employees-container {
  .staff {
    width: 70px;
    height: 70px;
    border-radius: 50%;
  }
}
#Turnofpage {
  height: 33px;
  width: 100%;
  position: relative;
  top: -10px;
  left: -10px;

  margin: 8px;
  border-bottom: 2px solid #ebeef5;
  // border: 1px solid #409eff;
}
#search {
  // line-height: 30px;
  // text-align: center;
  width: 100px;
  height: 18px;
  // background-color: pink;
  position: relative;
  top: 8px;
  left: 40px;
  i {
    font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
    color: #409eff;
  }

  samp {
    color: #409eff;
    // font-size: 1em;
    // font-family: monospace, monospace;

    font-size: 16px;
    font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
  }
}
</style>
